<?php

require_once __DIR__.DS.'blgData.php';

/**
 * This class represents a physical file.
 *
 * Open the file using the appropriate mode, and you will be able to
 * read and write to the file.
 */

class BlgMessage extends BlgData{
	protected $exists;	

	public function __construct($id){
		$this->initializeAttributes();
		$this->exists= $this->load($id);		
	}	
	
	/**
	 * Initializes file attributes
	 */
	protected function initializeAttributes() {
		parent::initializeAttributes();
		
		$time= time();
		
		$this->attributes['id'] = NULL;
		$this->attributes['from_guid'] = NULL;
		$this->attributes['to_guid'] = NULL;
		$this->attributes['subject'] = NULL;
		$this->attributes['message'] = NULL;
		$this->attributes['time_created'] = $time;
		$this->attributes['time_updated'] = $time;
		$this->attributes['status'] = '00000';
		$this->attributes['reply_id'] = NULL;		
		
		$this->exists= FALSE;		
	}

	/**
	 * Load message from database
	 */
	protected function load($id){
		$query= blg_dbschema_query_buid(blg_dbschema_select_messages(array('id' => $id)));
		$result= blg_dbschema_select($query);
		
		if($result->success){
			$this->attributes= $result->{0};
			return TRUE;
		}	
		
		return FALSE;
	}
	
	/**
	 * Save message to database
	 */
	public function save(){
		if($this->exists===TRUE){
			$query= blg_dbschema_query_buid(blg_dbschema_update_messages(
				array('id' => $this->attributes['id']),
				$this->attributes
			));
			$result= blg_dbschema_update($query);
			return $result->success;
		}
		
		return FALSE;
	}	
	
	/*** MASK OPERATIONS ***/
	
	public function delete_sender(){
		$this->attributes['status']= blg_messages_mask_ops('delete_sender', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}	
	
	public function undelete_sender(){
		$this->attributes['status']= blg_messages_mask_ops('undelete_sender', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}

	public function delete_recipient(){
		$this->attributes['status']= blg_messages_mask_ops('delete_recipient', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}	
	
	public function undelete_recipient(){
		$this->attributes['status']= blg_messages_mask_ops('undelete_recipient', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}
	
	public function delete(){
		$this->delete_sender();
		$this->delete_recipient();
	}	

	public function read_sender(){
		$this->attributes['status']= blg_messages_mask_ops('read_sender', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}
	
	public function unread_sender(){
		$this->attributes['status']= blg_messages_mask_ops('unread_sender', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}	
	
	public function read_recipient(){
		$this->attributes['status']= blg_messages_mask_ops('read_recipient', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}
	
	public function unread_recipient(){
		$this->attributes['status']= blg_messages_mask_ops('unread_recipient', $this->attributes['status']);
		$this->attributes['time_updated']= time();
		$this->save();		
	}
	
	
	public function exists(){ return $this->exists; }

	public function getURL(){
		//TODO Complete
		return blg_get_site_url().'';
	}
	
	/* STATIC */
	
	public static function create($from_guid, $to_guid, $subject, $message, $reply_id= null, $status= null){
		if(FALSE!==($message_id=blg_message_new($from_guid, $to_guid, $subject, $message, $reply_id, $status)))
			return new BlgMessage($message_id);
		
		return FALSE; 
	}	
}